package com.yfbao.horizon.demo;

import com.github.houbb.opencc4j.util.ZhConverterUtil;
import com.google.common.collect.Lists;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class HanToPinyinTest {
    private final static HanyuPinyinOutputFormat DEFAULT_FORMAT = new HanyuPinyinOutputFormat();

    public static void main(String[] args) {
//        test();
//        testStrToPinyin();
//        test2();
//        test3();
        mutilTest();
    }

    private static void mutilTest(){
        long start = System.currentTimeMillis();
        for( int i=0;i<10000;i++){
            test();
            test3();
        }
        System.out.println("cost:"+(System.currentTimeMillis()-start));
    }

    private static void test(){
        String[] res1= PinyinHelper.toHanyuPinyinStringArray('长');
        System.out.println(Arrays.toString(res1));
        String[] s = PinyinHelper.toHanyuPinyinStringArray('中');
        System.out.println(Arrays.toString(s));
        try {
            String ss = PinyinHelper.toHanYuPinyinString("长", DEFAULT_FORMAT, "", true);
            System.out.println(ss);
        } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
            badHanyuPinyinOutputFormatCombination.printStackTrace();
        }

        String[] ww = PinyinHelper.toHanyuPinyinStringArray('王');
        System.out.println(Arrays.toString(ww));

        String[] hh = PinyinHelper.toHanyuPinyinStringArray('和');
        System.out.println(Arrays.toString(hh));

    }

    private static void test3(){
        String str = "鋸子汽水";
        String str1= "中國";
        System.out.println(ZhConverterUtil.toTraditional(str));
        System.out.println(ZhConverterUtil.toTraditional(str1));
        System.out.println("---------------------");
        System.out.println(ZhConverterUtil.toSimple(str));
        System.out.println(ZhConverterUtil.toSimple(str1));
        String aa = "{\"trigger\": \"date\", \"run_date\": \"2025-02-21 16:50:00\"}";
    }

    private static void test2(){
        Random random = new Random();
        int randomCodePoint = random.nextInt(0x9fa5 - 0x4e00 + 1) + 0x4e00;
        char randomChineseCharacter = (char) randomCodePoint;
//        System.out.println("随机生成的汉字：" + randomChineseCharacter);
        char[] chineseCharacters = {'中', '国', '人', '民', '大', '学', '天', '地', '山', '水', '木', '火', '土', '日', '月', '风', '云', '雨', '雪', '雷'};
        List<String> list =new ArrayList<>();
        long beginFactoryWords = System.currentTimeMillis();
        for(int i=0;i<200;i++){
            int k = random.nextInt(10);
            StringBuilder str =new StringBuilder();
            if(k<1){
                k=1;
            }
            for(int j=0;j<k;j++){
//                str.append((char)(random.nextInt(0x9fa5 - 0x4e00 + 1) + 0x4e00));

                int randomIndex = random.nextInt(chineseCharacters.length);
                str.append(chineseCharacters[randomIndex]);
            }
//            System.out.println("随机生成的汉字：" + str);
            list.add(str.toString());
        }
        System.out.println("FactoryWords cost:"+(System.currentTimeMillis()-beginFactoryWords));
        long beginTopinyin = System.currentTimeMillis();
        for (String s : list) {
            try {
                PinyinHelper.toHanYuPinyinString(s, DEFAULT_FORMAT, "", true);
            } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
                badHanyuPinyinOutputFormatCombination.printStackTrace();
            }
        }
        System.out.println("topinyin cost:"+(System.currentTimeMillis()-beginTopinyin));
    }

    private static void testStrToPinyin(){
        String str ="a好12?";
        List<String> list = toPinyinWord(str);
        System.out.println(list);
    }

    private static List<String> toPinyinWord(String word){
        List<List<String>> list =new ArrayList<>();
        boolean hasHan=false;
        for (char c : word.toCharArray()) {
            List<String> indexList = new ArrayList<>();
            list.add(indexList);

            String[] strings = PinyinHelper.toHanyuPinyinStringArray(c);
            if(strings ==null || strings.length==0){
                indexList.add(String.valueOf(c));
                continue;
            }
            for (String str : strings) {
                if(str.equals("none0")){
                    continue;
                }
                hasHan = true;
                String substring = str.substring(0, str.length() - 1);
                if(!indexList.contains(substring)){
                    indexList.add(substring);
                }
            }
            if(indexList.isEmpty()){
                indexList.add(String.valueOf(c));
            }
        }
        if(!hasHan){
            //考虑返回null 或者空对象
            return Lists.newArrayList();
        }

        return getPinyin(list,list.size()-1,Lists.newArrayList());
    }

    private static List<String> getPinyin(List<List<String>> list, int index,List<String> res){
        if(index<0){
            return res;
        }
        List<String> indexList = list.get(index);

        if(res.isEmpty()){
            return getPinyin(list,index-1,indexList);
        }
        List<String> rs = new ArrayList<>();
        for (String s : indexList) {
            for (String re : res) {
                rs.add(s+re);
            }
        }
        return  getPinyin(list,index-1,rs);
    }
}
